<template>
  <div class="bottom-menu">
    <CheckButton class="select-all" @checkBtnClick="checkBtnClick" v-model="isSelectAll"></CheckButton>
    <span>全选</span>
    <span class="total-price">合计: ¥{{totalPrice}}</span>
    <span class="buy-product" @click="calcClick">去计算({{clickLength}})</span>
  </div>
</template>

<script>
import CheckButton from './CheckButton'
import { mapGetters } from 'vuex'

export default {
  name: "BottomBar",
  components: {
    CheckButton
  },
  computed: {
    ...mapGetters([
      'cartList',
      'cartLength'
    ]),
    totalPrice() {
      const cartList = this.cartList;
      return cartList.filter(item => {
        return item.checked
        // preValue表示初始值
      }).reduce((preValue, item) => {
        return preValue + item.count * item.price
        // 表示保留两位小数
      }, 0).toFixed(2)
    },
    clickLength() {
      // 此方法是上面totalPrice的简写
      return this.cartList.filter(item => item.checked).reduce((preValue, item) => preValue + item.count,0)
    },
    isSelectAll() {
      if(this.cartList.length === 0 ) return false
      // 查找到不选中的商品则整体不选中
      return !this.cartList.find(item => !item.checked) ;
    }
  },
  methods: {
    checkBtnClick() {
      // 2.如果商品全部按钮选中, 则设置为不选中
      if (this.isSelectAll) {
        this.cartList.forEach(item => {
          item.checked = false;
        });
        // 如果有任意一个按钮不选中，则设置为全部选中
      } else {
        this.cartList.forEach(item => {
          item.checked = true;
        });
      }
    },
    calcClick() {
      if(!this.isSelectAll) {
        this.$toast.show('请选择购买的商品' , 2000)
      }
    }
  }
}
</script>

<style scoped>
.bottom-menu {
  width: 100%;
  height: 44px;
  background-color: #eee;
  position: fixed;
  bottom: 46px;
  left: 0;
  box-shadow: 0 -2px 3px rgba(0, 0, 0, .2);
  font-size: 14px;
  color: #888;
  line-height: 44px;
  padding-left: 35px;
  box-sizing: border-box;
}

.bottom-menu .select-all {
  position: absolute;
  line-height: 0;
  left: 12px;
  top: 13px;
}

.bottom-menu .total-price {
  margin-left: 15px;
  font-size: 16px;
  color: #666;
}

.bottom-menu .buy-product {
  background-color: orangered;
  color: #ffffff;
  width: 100px;
  height: 44px;
  text-align: center;
  line-height: 44px;
  float: right;
}
</style>
